/**
 * 输入域构造器
 */
function sInput(target, type, content){
    if(this.initialize) return;

    var thiz = target ? target : this;
    
    sElement.call(this, type);

    var style = thiz.style ? thiz.style : {};

    var attributes = {
        ...thiz.attributes
    };

    this.id = attributes.id;
    this.name = attributes.name;

    /**
     * 定义attributes
     */
    Object.defineProperty(this, "style", { 
        get: function(){
            return style;
        },
        set: function(val){
            if(!val || typeof(val) !== "object") return;
            for(let i in val){
                style[i] = val[i];
            }
        },
        enumerable: true
    });

    var display = style.display;
    delete style.display;

    /**
     * 定义样式的display
     */
    Object.defineProperty(this.style, "display", {
        get: ()=>{
            return display;
        },
        set: (value)=>{
            let visibleChange = display != value;
            
            display = value;

            if(visibleChange){
                if(this.__beginTag){
                    this.__beginTag.style.display = value;
                    let next = this.__beginTag.__nextSibling;
                    while(next){
                        next.style.display = value;
                        if(next.__name == this.__name) break;
                        next = next.__nextSibling;
                    }
                }else if(this.__endTag){
                    this.__endTag.style.display = value;
                }

                this.dispatchEvent("visibleChange", {
                    target: this                    
                });
            }
        },
        enumerable: true
    });

    /**
     * 定义attribute
     */
    Object.defineProperty(this, "attributes", { 
        get: ()=>{
            return attributes;
        },
        set: (val)=>{
            if(!val || typeof(val) !== "object") return;
            updateAttributes.call(this, val);
        },
        enumerable: true
    });

    /**
     * 更新属性
     * @param attrs
     */
    var updateAttributes = function(attrs){
        attributes = {...attributes, ...attrs};
        
        this.id = attributes.id;
        this.name = attributes.name;

        //结束标签元素
        if(this.__endTag){
            this.__endTag.attributes = attrs;
        }

        //背景文本元素
        // if(this.__placeholder){
        //     this.__placeholder.innerText = attributes.placeholder;
        // }
    }
    

    var text = new sText();
    text.innerText = content;

    /**
     * 显示的内容
     */
    Object.defineProperty(this, "tagView", {
        get: function(){
            text.__paragraphIndex = this.__paragraphIndex;
            return text;
        }
    });

    var isText = this.tagView._sType == "sText";

    Object.defineProperty(this, "simulatedText", {
        get: function(){
            return isText;
        }
    });
}

/**
 * 输入域起始标签元素
 */
function sInputBegin(target){
    return new sInput(target, "sInputBegin", "[");
}

/**
 * 输入域结束标签元素
 */
function sInputEnd(target){
    return new sInput(target, "sInputEnd", "]");
}

/**
 * 将sElement原型上的方法和属性赋值给sInput原型
 */
for(let i in sElement.prototype){
    sInput.prototype[i] = sElement.prototype[i];
}
